package com.lnkj.spcj.ui.splash

import android.Manifest
import android.content.Context
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseActivity
import com.lnkj.spcj.ui.scan.ScanActivity
import com.lnkj.spcj.util.utilcode.BarUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_splash.*
import org.jetbrains.anko.startActivity

class SplashActivity : BaseActivity<SplashContract.Present>(), SplashContract.View {

    override val mPresenter: SplashContract.Present
        get() = SplashPresent().also { it.attachView(this) }
    override val layoutRes: Int
        get() = R.layout.activity_splash

    override fun initAll() {
        BarUtils.setStatusBarAlpha(this, 0)
        val animation = AlphaAnimation(1.0f, 1.0f)
        animation.duration = 0
        iv_splash.startAnimation(animation)
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(arg0: Animation) {
                requestPermission()
            }

            override fun onAnimationRepeat(animation: Animation) {

            }

            override fun onAnimationStart(animation: Animation) {

            }
        })
    }

    override fun setListener() {

    }

    override fun processLogic() {

    }

    override fun getContext(): Context {
        return mContext
    }

    override fun onEmpty() {

    }

    override fun onError() {

    }

    fun requestPermission() {
        RxPermissions(this@SplashActivity)
                .request(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO)
                .subscribe {
                    if (it) {
                        startActivity<ScanActivity>()
                        finish()
                    } else {
                        ToastUtils.showShort("请开启相应权限")
                    }
                }
    }
}
